package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.Employee;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DishServiceImpl  extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1.先保存菜品
        this.save(dishDto);
        //2.获取id
        Long dishId=dishDto.getId();
        //3.存储口味信息
        List<DishFlavor> list=new ArrayList<>();
        List<DishFlavor> flavors = dishDto.getFlavors();
        log.info(flavors.toString());
        for (DishFlavor dish :
                flavors) {
            dish.setDishId(dishId);
            list.add(dish);
        }
        //4.批量存储
        log.info(list.toString());
        dishFlavorService.saveBatch(list);
    }

    @Override

    public DishDto getDishDto(Long id) {
        Dish dish = this.getById(id);
        //通过菜品id查询口味信息
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);

        List<DishFlavor> list = dishFlavorService.list(qw);

        DishDto dishDto=new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishDto(DishDto dishDto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        //保存菜品信息
        this.updateById(dish);

        //口味信息
        List<DishFlavor> list=dishDto.getFlavors();

        //根据菜品信息查询口味信息  如果是null 直接保存  如果不是null 更新
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dishDto.getId());
        log.info(dishDto.getId().toString());
        dishFlavorService.remove(qw);
        log.info("更新的口味信息："+list.toString());

        List<DishFlavor> dishFlavors=new ArrayList<>();
        for (DishFlavor d: list) {
            d.setDishId(dishDto.getId());
            dishFlavors.add(d);
        }
        dishFlavorService.saveBatch(dishFlavors);
        log.info("222");
    }

    //删除方法
    @Transactional
    @Override
    public void delete(Long ids) {
        //删除菜品信息
        this.removeById(ids);
        //删除口味信息
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,ids);
        List<DishFlavor> list=dishFlavorService.list(qw);
        for (DishFlavor dishFlavor:
             list) {
            Long id=dishFlavor.getId();
            dishFlavorService.removeById(id);
        }
    }
}

